<?php

namespace Admin\Model\DAO;

use Zend\Db\TableGateway\AbstractTableGateway;
use Admin\Entity\SerialBook;
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\Sql\Sql;
use Zend\Db\Sql\Where;
use Zend\Db\Adapter\Adapter;

class HistoryCiculationTable extends AbstractTableGateway {

    protected $table;
    protected $adapter;
    protected $resultSetPrototype;

    public function __construct(Adapter $adapter) {
        $this->table = 'lib_historyciculations';
        $this->adapter = $adapter;
        $this->resultSetPrototype = new ResultSet();
    }

    public function saveData($data) {
        // Đây là một quá trình tự động nên không cần kiểm tra khi insert
        // Lỗi chỉ xảy ra khi có sự cố về kết nối mạng hoặc đồng bộ dữ liệu
        $this->insert($data);
    }

    public function getHistoryCiculation($dateTime, $status) {
        $sql = new Sql($this->adapter);
        $select = $sql->select();
        $select->columns(array('timeSave', 'librarianName'))
                ->from(array('hc' => $this->table))
                ->join(array('sb' => 'lib_serialBooks'), 'sb.bookID = hc.bookID', array('serial'), 'left')
                ->join(array('rd' => 'lib_readers'), 'rd.cardBarcode = hc.cardBarcode', array('cardBarcode'), 'left')
                ->join(array('bs' => 'lib_bookStores'), 'sb.bookBarcode = bs.bookBarcode', array('*'), 'left')
                ->join(array('us' => 'lib_users'), 'us.userID = rd.userID', array('fullName', 'residentAddress'), 'left');
        $where = new Where();
        $where->equalTo('timeSave', $dateTime)
                ->AND
                ->equalTo('historyCiculationStatus', $status);
        $select->where($where);
        $statement = $sql->prepareStatementForSqlObject($select);
        $result = $statement->execute();
        $resultSet = clone $this->resultSetPrototype;
        $resultSet->initialize($result);
        $resultSet->buffer();
        $resultSet->next();
        return $resultSet;
    }
    public function getHistoryCiculationInMonth($dateTime, $status) {
        $sql = new Sql($this->adapter);
        $select = $sql->select();
        $select->columns(array('timeSave', 'librarianName'))
                ->from(array('hc' => $this->table))
                ->join(array('sb' => 'lib_serialBooks'), 'sb.bookID = hc.bookID', array('serial'), 'left')
                ->join(array('rd' => 'lib_readers'), 'rd.cardBarcode = hc.cardBarcode', array('cardBarcode'), 'left')
                ->join(array('bs' => 'lib_bookStores'), 'sb.bookBarcode = bs.bookBarcode', array('*'), 'left')
                ->join(array('us' => 'lib_users'), 'us.userID = rd.userID', array('fullName', 'residentAddress'), 'left');
        $where = new Where();
        $where->greaterThan('timeSave', $dateTime)
                ->AND
                ->equalTo('historyCiculationStatus', $status);
        $select->where($where);
        $statement = $sql->prepareStatementForSqlObject($select);
        $result = $statement->execute();
        $resultSet = clone $this->resultSetPrototype;
        $resultSet->initialize($result);
        $resultSet->buffer();
        $resultSet->next();
        return $resultSet;
    }
}